package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.demo.core.dao.FeedbackDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Feedback;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;
import com.demo.core.service.base.FeedbackService;

public class FeedbackServiceImpl implements FeedbackService {

	/**
	 * 回复/反馈数据访问层接口
	 */
	private FeedbackDao feedbackDao;
	
	@Override
	public Long save(Feedback t) {
		return feedbackDao.insert(t);
	}

	@Override
	public boolean exists(Long id) {
		return feedbackDao.exists(id);
	}

	@Override
	public void delete(Long id) {
		feedbackDao.delete(id);
	}

	@Override
	public void update(Long id, Feedback feedback) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		feedbackDao.update(feedback, params);
	}

	@Override
	public void update(Long id, int checkStep, Boolean isChecked) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		Feedback feedback = new Feedback();
		feedback.setCheckStep(checkStep);
		feedback.setChecked(isChecked);
		feedbackDao.updateStep(feedback, params);
	}
	
	@Override
	public Feedback find(Long id) {
		return feedbackDao.select(id);
	}

	@Override
	public List<Feedback> findList(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		return feedbackDao.select(params); 
	}

	@Override
	public Feedback tree(Long id, int... depths) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		if (depths != null && depths.length > 0) {
			List<?> depthList = CollectionUtils.arrayToList(depths);
			params.put("depthList", depthList);
		}
		List<Feedback> feedbackList = feedbackDao.tree(params);
		
		return AdjacencyListToChainedObjectConverter.conver(feedbackList);
	}

	@Override
	public Feedback tree(Long id) {
		return tree(id);
	}

	@Override
	public ResultSearchResult<Feedback> search(Long siteId, int firstResult, int maxResults) {
		if (firstResult < 0 || maxResults < 1) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);

		return feedbackDao.search(params, new SearchArgumentSupport(firstResult, firstResult));
	}

	@Override
	public ResultSearchResult<Feedback> search(Long siteId, String type, int firstResult, int maxResults) {
		if (firstResult < 0 || maxResults < 1) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);

		return feedbackDao.search(params, new SearchArgumentSupport(firstResult, firstResult));
	}
	
	@Override
	public ResultSearchResult<Feedback> search(Long siteId, String type, SearchArgumentSupport args) {
		if (args.getFirstResult() < 0 || args.getMaxResults() < 1) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);

		return feedbackDao.search(params, args);
	}
	
	public void setFeedbackDao(FeedbackDao feedbackDao) {
		this.feedbackDao = feedbackDao;
	}
}
